Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds url whitelist regex to ext options #410

Merged
merged 4 commits into from
Feb 8, 2024
Merged

Conversation

Jyyjy
Copy link
Contributor

@Jyyjy Jyyjy commented Feb 7, 2024

Adds an extension option to filter urls. Only urls that match the regex will be logged, this includes tab events.

By default the extension will block all urls. Currently all urls are logged by default, but this change is intended to protect privacy.

Tagging @EandrewJones for review

Copy link
Contributor

@EandrewJones EandrewJones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized I left comments on build instead of src. Nonetheless, there are a few small changes otherwise looks good.

Setting the default to log nothing to enforce opt-in is a very nice touch.

* Adds named callbacks to be executed when logging.
* @param {Object } newCallbacks An object containing named callback functions.
*/
function addCallbacks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments to this function to help describe what's happening here at a business logic level.

return false;
}
browser.storage.local.get(defaultConfig, function (res) {
addCallbacks({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again please add brief explanation of why we need add the filterUrl as a callback since in the lines right below we apply the function directly in the browser event handler.

function dispatchTabMessage(message) {
browser.tabs.query({}, function (tabs) {
tabs.forEach(function (tab) {
browser.tabs.sendMessage(tab.id, message);
});
});
}
function filterUrl(log) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code smell to have two different return types depending on conditional. Since you're not actually modifying the log, you don't need to return the log.

Better to have function name like isUrlWhitelisted which only returns bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is a little wonky because it has to match the pattern expected by addcallbacks(). Callbacks are called by package log functions and modify logs or drop them if given a falsey value. I've already created a ticket to change this callback functionality to match the pattern you used for modifying headers. Once the core api is changed, we can modify how we're using it here.

Here's some background on why addcallbacks is the way it is:
#312
#350

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.

browser.runtime.onMessage.addListener(function (message) {
switch (message.type) {
// Handles logs rerouted from content and option scripts
case ADD_LOG:
log(message.payload);
var log$1 = filterUrl(message.payload);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then here, you just check if it's whitelisted. if not, return early, otherwise proceed.

if !isUrlWhitelisted(message.payload) return
log(message.payload)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an artifact of unclear behavior of the current logging callback api. Callbacks are not executed by log(), so I am mimicking the handling of callback results in packagelog() here in the message handler. This is so there can be a single place where URL filtering logic is defined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

options(config);
browser.runtime.sendMessage({
type: CONFIG_CHANGE,
payload: config
payload: payload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: JS will automatically infer the key if your value of the same name.

So {type: CONFIG_CHANGE, payload } is perfectly valid. Saves you a few characters of typing.

@Jyyjy Jyyjy closed this Feb 8, 2024
@Jyyjy Jyyjy reopened this Feb 8, 2024
@Jyyjy Jyyjy merged commit aac2dce into apache:test Feb 8, 2024
2 checks passed
@Jyyjy Jyyjy deleted the urlfilter branch February 8, 2024 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants